package com.lmscn.lms.api.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class KitUtil {
	
	public static List<String> splitToStringList(String str){
		return splitToStringList(str,",");
	}
	
	public static List<String> splitToStringList(String str,String regex){
		String [] strs = splitToStringArray(str,regex);
		if(strs == null){
			return null;
		}
		List<String> longList = new ArrayList<String>();
		longList.addAll(Arrays.asList(strs));
		return longList;
	}
	
	public static String [] splitToStringArray(String str){
		return splitToStringArray(str,",");
	}
	
	public static String [] splitToStringArray(String str,String regex){
		if(str == null || str.trim().length() == 0){
			return null;
		}
		if(",".equals(regex)){
			str = str.replaceAll("，", ",").replaceAll(" ", "");
		}
		String [] arr = str.split(regex);
		return arr;
	}
	
	/**
	 * @Author:ZhangHaiwei
	 * @Description:将字符创转换成为Long类型数组，默认使用逗号分割
	 * @param str	要被分割的字符串
	 * @return
	 */
	public static Long [] splitToLongArray(String str){
		return splitToLongArray(str,",");
	}
	
	/**
	 * @Author:ZhangHaiwei
	 * @Description:将字符创转换成为Long类型列表，默认使用逗号分割
	 * @param str	要被分割的字符串
	 * @return
	 */
	public static List<Long> splitToLongList(String str){
		if(str == null || str.trim().length() == 0){
			return null;
		}
		return splitToLongList(str,",");
	}
	
	public static Long [] splitToLongArray(String str,String regex){
		if(str == null || str.trim().length() == 0){
			return null;
		}
		Long[] longs;
		try {
			if(",".equals(regex)){
				str = str.replaceAll("，", ",").replaceAll(" ", "");
			}
			String [] arr = str.split(regex);
			longs = new Long[arr.length];
			int count = 0;
			for(int i=0;i<arr.length;i++){
				if(arr[i].trim().length() != 0){
					longs[count] = Long.parseLong(arr[i]); 
					count ++;
				}
			}
			return longs;
		} catch (NumberFormatException e) {
			//e.printStackTrace();
			return null;
		}
		
	}
	
	public static List<Long> splitToLongList(String str,String regex){
		Long [] longs = splitToLongArray(str,regex);
		if(longs == null){
			return null;
		}
		List<Long> longList = new ArrayList<Long>();
		longList.addAll(Arrays.asList(longs));
		return longList;
	}

}
